import os
import pandas as pd
from pprint import pprint
import pymysql

class DataOperation:
    def __init__(self,data, path='',sql='', host='localhost', user='root', password='root', database='it', charset='utf8'):
        # 打开csv文件
        if data == 'csv':
            self.data = pd.read_csv(path)
            # 打开excle
        elif data == 'excel':
            self.data = pd.read_excel(path)
            # 打开sql
        elif data == 'mysql':
            conn = pymysql.connect(host=host, user=user, password=password, database=database, charset=charset)
            self.data = pd.read_sql(sql, conn)

    def get_data_to_dict(self):
        # 以字典形式读取数据
       return [self.data.loc[i].to_dict() for i in self.data.index.values]

    def get_data_to_list(self):
        # 以表格形式·读取数据
        return self.data.values.tolist()


if __name__ == '__main__':
    # data = DataOperation('csv', '..\\data\\lj_data.csv')
    # pprint(data.get_data_to_dict())
    # pprint(data.get_data_to_list())
    # data = DataOperation('excel', '..\\data\\user_login.xls')
    # pprint(data.get_data_to_dict())
    # pprint(data.get_data_to_list())
    data = DataOperation('mysql', sql = 'select * from student')
    pprint(data.data.head())




